import request from './request'

//获取token
function getToken(){


    return localStorage.getItem('token');
}


export async function post(operate,isAuth=true,isBody=true){


    operate.method = 'POST';

    if (isAuth){
       let token =  getToken();
       operate.headers = {...operate.headers,'Authorization':token}
    }
    let contentType = '';
    if (isBody){
        contentType = 'application/json'

    }else {
         contentType = 'application/x-www-form-urlencoded'
    }


    operate.headers = {...operate.headers,'Content-Type':contentType}



    const {data} = await request(operate)

    return data;

}

export async function get(operate,isAuth=true){

    operate.method = 'GET';
    if (isAuth){
       const token =  getToken();

       operate.headers = {'Authorization':token,...operate.headers}

    }
    let {data} = await request(operate)

    // console.log('data',data)


    return data;
}

export async function upload(operate,isAuth=true){
    operate.method = 'POST';
    operate.headers = {'Content-Type':'multipart/form-data',...operate.headers};

    //判断是都需要认证
    if (isAuth){
        const token =  getToken();
        operate.headers = {'Authorization':token,...operate.headers};
    }

    let {data} = await request(operate);

    return data;
}